package OddMuse::Searching;

=head1 NAME

OddMuse::Searching - Perl Module for OddMuse searching classes

=head1 DESCRIPTION

This module provides a universal search method for Oddmuse which allows for
very fast searching.

=head1 DEPENDENCIES

L<OddMuse::Database>

=cut

use OddMuse::Database;
$VERSION   = ( q$Revision: 30 $ ) =~ /(\d+)/g;
use strict; # Because ...

=head1 SYNOPSIS

To search and print out some results (without formatting or any other fancy
html) try:

  use OddMuse::Searching;
  my $searchresult = OddMuse::Searching->new($searchtext);

  print "These pages contain $searchtext";

  for my $pages ( keys %{$searchresult} ) {
      if ($searchresult->{$pages}{'wikiname'}) {
         print "$searchresult->{$pages}{'wikiname'}\n";
      }
  }

=head1 METHODS

=over 4

=item * $object->new( 'searchterm' );

Connects to the datastore and retrieves the search results, and returns a
fairly complex hash structure.  The format for the returned hash reference is
as follows.

 WikiName = ( 
     wikiname       => 'WikiName',       
     title          => 'Title of Page', 
     subtitle       => 'A longer description', 
     recentauthor   => 'UserName',
     recentedit     => 1159367500,
     pagesize       => 1534,
     first          => 'A summary ...', 
     type           => 'result_words'
 ) 

The hash key is the WikiName.  Title and Subtitle refer to the "SmartTitle" and
the "Smart SubTitle".  Recent Author is a username.  RecentEdit is the last
edited date in epoch.  Pagesize is in bytes.  First and words are simply long
strings of text.

Type will be one of the following.

# result_words = Match found in page body
# result_title = Match found in page Smart Title
# result_subtitle = Mauch found in Smart Sub Title
# result_name = Mauch found in WikiName

=back

=cut

sub new {
    my ( $class, $searchtext ) = @_;
    my $links  = {};                         # Anon Array
    my %self   = {};                         # Anon Array
    my $ondisk = OddMuse::Database->new();

    while ( my ( $source, $attribute ) = each %{ $ondisk } ) {
      next if $ondisk->{ $source }{ 'redirects' };
      next
          if ( $ondisk->{ $source }{ 'wikiname' } =~ /$OddMuse::SearchBanned/ );

        if ( $ondisk->{ $source }{ 'wikiname' } =~ /$searchtext/i ) {

            # Match based on page wikiname
            my $wikiname     = $ondisk->{ $source }{ 'wikiname' };
            my $title        = $ondisk->{ $source }{ 'title' };
            my $subtitle     = $ondisk->{ $source }{ 'subtitle' };
            my $recentauthor = $ondisk->{ $source }{ 'recentauthor' };
            my $recentedit   = $ondisk->{ $source }{ 'recentedit' };
            my $pagesize     = $ondisk->{ $source }{ 'pagesize' };
            my $first        = $ondisk->{ $source }{ 'first' };
            my $link         = $self{ $source };
            $link->{ 'wikiname' }     = $wikiname;
            $link->{ 'title' }        = $title;
            $link->{ 'subtitle' }     = $subtitle;
            $link->{ 'first' }        = $first;
            $link->{ 'recentauthor' } = $recentauthor;
            $link->{ 'recentedit' }   = $recentedit;
            $link->{ 'pagesize' }     = $pagesize;
            $link->{ 'type' }         = 'result_name';
            $self{ $source }          = $link;

        } elsif ( $ondisk->{ $source }{ 'title' } =~ /$searchtext/i ) {

            #  Found in title
            my $wikiname     = $ondisk->{ $source }{ 'wikiname' };
            my $title        = $ondisk->{ $source }{ 'title' };
            my $subtitle     = $ondisk->{ $source }{ 'subtitle' };
            my $recentauthor = $ondisk->{ $source }{ 'recentauthor' };
            my $recentedit   = $ondisk->{ $source }{ 'recentedit' };
            my $pagesize     = $ondisk->{ $source }{ 'pagesize' };
            my $first        = $ondisk->{ $source }{ 'first' };
            my $link         = $self{ $source };
            $link->{ 'wikiname' }     = $wikiname;
            $link->{ 'title' }        = $title;
            $link->{ 'subtitle' }     = $subtitle;
            $link->{ 'first' }        = $first;
            $link->{ 'recentauthor' } = $recentauthor;
            $link->{ 'recentedit' }   = $recentedit;
            $link->{ 'pagesize' }     = $pagesize;
            $link->{ 'type' }         = 'result_title';
            $self{ $source }          = $link;

        } elsif ( $ondisk->{ $source }{ 'subtitle' } =~ /$searchtext/i ) {

            # Found in subtitle
            my $wikiname     = $ondisk->{ $source }{ 'wikiname' };
            my $title        = $ondisk->{ $source }{ 'title' };
            my $subtitle     = $ondisk->{ $source }{ 'subtitle' };
            my $recentauthor = $ondisk->{ $source }{ 'recentauthor' };
            my $recentedit   = $ondisk->{ $source }{ 'recentedit' };
            my $pagesize     = $ondisk->{ $source }{ 'pagesize' };
            my $first        = $ondisk->{ $source }{ 'first' };
            my $link         = $self{ $source };
            $link->{ 'wikiname' }     = $wikiname;
            $link->{ 'title' }        = $title;
            $link->{ 'subtitle' }     = $subtitle;
            $link->{ 'first' }        = $first;
            $link->{ 'recentauthor' } = $recentauthor;
            $link->{ 'recentedit' }   = $recentedit;
            $link->{ 'pagesize' }     = $pagesize;
            $link->{ 'type' }         = 'result_subtitle';
            $self{ $source }          = $link;

        } elsif ( $ondisk->{ $source }{ 'words' } =~ /$searchtext/i ) {

            # Found in page content itself
            my $wikiname     = $ondisk->{ $source }{ 'wikiname' };
            my $title        = $ondisk->{ $source }{ 'title' };
            my $subtitle     = $ondisk->{ $source }{ 'subtitle' };
            my $recentauthor = $ondisk->{ $source }{ 'recentauthor' };
            my $recentedit   = $ondisk->{ $source }{ 'recentedit' };
            my $pagesize     = $ondisk->{ $source }{ 'pagesize' };
            my $first        = $ondisk->{ $source }{ 'first' };
            my $link         = $self{ $source };
            $link->{ 'wikiname' }     = $wikiname;
            $link->{ 'title' }        = $title;
            $link->{ 'subtitle' }     = $subtitle;
            $link->{ 'first' }        = $first;
            $link->{ 'recentauthor' } = $recentauthor;
            $link->{ 'recentedit' }   = $recentedit;
            $link->{ 'pagesize' }     = $pagesize;
            $link->{ 'type' }         = 'result_words';
            $self{ $source }          = $link;
        } ## end elsif ( $ondisk->{ $source...
    } ## end while ( ( $source, $attribute...

  return bless \%self;
} ## end sub new

1;

__END__

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<cmauch@gmail.com>

=head1 AUTHOR

Charles Mauch <cmauch@gmail.com>

=head1 LICENSE

Copyright (c) 2006 Charles Mauch

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 SEE ALSO

perl(1).  OddMuse::Database(1).

=cut

# $Id: Searching.pm 30 2006-09-29 06:19:20Z cmauch $
